package com.project.nimsen.web.pub;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.project.jdbc.utils.RegexValidateUtils;
import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.entity.TMerberEntity;
import com.project.nimsen.service.IMemberService;
import com.project.nimsen.util.WebUtil;
@Controller
@RequestMapping(value ="/pub")
public class PubMemberControl extends BaseMVCAction {
	@Resource
	IMemberService memberService;
	@Resource
	WebUtil webUtil;
	@RequestMapping(value="/mem/*")
	public String action(){
		//request.getContextPath()
		String url = getRequest().getRequestURI();
		if("/".equals(url.substring(url.length()-1))){
			url = url.substring(0, url.length()-1);
		}
		String id = url.substring(url.lastIndexOf("/")+1);
		try {
			TMerberEntity ent = memberService.findByID(id);
			if(ent==null){
				return "error/404";
			}
			getRequest().setAttribute("truename", ent.getM_TRUE_NAME());
			getRequest().setAttribute("pid", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pub/reg";
	}
	//
	@RequestMapping(value="/memb/zzlc")
	public String zzlc(){
		return "pub/zzlc";
	}
	@RequestMapping(value="/memb/add")
	public String addmem(){
		String name = getParameter("name");
		if(StringUtils.isBlank(name)){
			getRequest().setAttribute("message", "姓名不可以为空");
			return "pub/reg";
		}
		/*String card_id = getParameter("card_id");
		if(!RegexValidateUtils.isCardid(card_id)){
			getRequest().setAttribute("message", "身份证格式不正确");
			return "pub/reg";
		}*/
		String weixin = getParameter("weixin");
		if(StringUtils.isBlank(weixin)){
			getRequest().setAttribute("message", "微信号不可以为空");
			return "pub/reg";
		}
		String mphone = getParameter("mphone");
		if(!RegexValidateUtils.isMphone(mphone)){
			getRequest().setAttribute("message", "手机号格式不正确");
			return "pub/reg";
		}
		/*String bank_card_id = getParameter("bank_card_id");
		if(StringUtils.isNotBlank(bank_card_id)){
			if(!RegexValidateUtils.isbankcardid(bank_card_id)){
				getRequest().setAttribute("message", "银行卡号格式不正确");
				return "pub/reg";
			}
		}*/
		Date now = new Date();
		TMerberEntity ent = new TMerberEntity();
		ent.setACTIVE(0);
		ent.setREG_TIME(now);
		ent.setPID(getParameter("pid"));
		//ent.setBANK_CARD_ID(bank_card_id);
		//ent.setCARD_ID(card_id);
		ent.setM_TRUE_NAME(name);
		ent.setMPHONE(mphone);
		ent.setWEIXIN(weixin);
		ent.setCREATE_TIME(now);
		ent.setMESSAGE(getParameter("r_message"));
		ent.setFLOWING_WATER(getParameter("flowater"));
		try {
			//数据重复验证
			String message = memberService.findVal(ent);
			if(!"true".equals(message)){
				getRequest().setAttribute("message", message);
				return "pub/reg";
			}
			ent.setCODES(memberService.findNextCode(ent.getPID()));
			Object obj_id = memberService.save(ent);
			if(obj_id!=null){
				getRequest().setAttribute("url", webUtil.webUrl(getRequest())+"/pub/mem/"+obj_id.toString());
				return "pub/reg_suc";
			}else{
				getRequest().setAttribute("message", "服务器出问题请联系管理员");
				return "pub/reg";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
